package com.rocky.ssm.controller;

import com.rocky.ssm.model.Student;
import com.rocky.ssm.service.IStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * Created by Administrator on 2017/8/15.
 */

@Controller
@RequestMapping("/students")
public class StudentsController {

    @Autowired
    private IStudent student;

    @RequestMapping("/studentsList")
    public String list(Model model){
        List<Student> list = student.getStuList();
        model.addAttribute("list",list);
        return "students/list";
    }

    @RequestMapping("/{id}/studentsUpdate")
    public String update(@PathVariable int id, Model model){
        Student s = student.getTalkById(id);
        model.addAttribute("students",s);
        return "students/update";
    }

    @RequestMapping(value = "/doStudentsUpdate",method = RequestMethod.POST)
    public String doUpdate(Student s){
        if(student.update(s)>0)
            return "redirect:/students/studentsList";
        else
            return "students/update";
    }

    @RequestMapping("/{id}/studentsDel")
    public String delete(@PathVariable int id){
        student.delete(id);
        return "redirect:/students/studentsList";
    }

    @RequestMapping("skipStudentsAdd")
    public String skipAdd(){
        return "students/add";
    }

    @RequestMapping(value = "/studentsAdd",method = RequestMethod.POST)
    public String add(Student s){
        if(student.save(s)>0)
            return "redirect:/students/studentsList";
        else
            return "students/add";

    }


}
